package user;

import org.apache.log4j.Logger;
import java.util.ArrayList;

import java.text.SimpleDateFormat;
import java.util.Date;

import utils.Encryptor;



public class UserBean {
	
	private static Logger jlog = Logger.getLogger(UserBean.class);
	
	private UserBeanDAO ubdao;
	private ArrayList<UserBean> usrLst;

	private String username;
	private String password;
	private String fullname;
	private String location;
	private boolean admin;
	private boolean inactive;
	
	private boolean loggedIn = false;
	
	public UserBean()
	{
		this.ubdao = new UserBeanDAO();
	}
	
	public boolean isLoggedIn()
	{
		return this.loggedIn;
	}

	
	public String getUsername()
	{
		return this.username;
	}
	public void setUsername(String user)
	{
		this.username = user;
	}
	
	public String getPassword()
	{
		return this.password;
	}
	public void setPassword(String pwd)
	{
		this.password = pwd;
	}
	
	public String getFullname()
	{
		return this.fullname;
	}
	public void setFullname(String name)
	{
		this.fullname = name;
	}
	
	public String getLocation()
	{
		return this.location;
	}
	public void setLocation(String location)
	{
		this.location = location;
	}
	
	public boolean isAdmin()
	{
		return this.admin;
	}
	public void setAdmin(boolean admin)
	{
		this.admin = admin;
	}
	
	public boolean isInactive()
	{
		return this.inactive;
	}
	public void setInactive(boolean inactive)
	{
		this.inactive = inactive;
	}
	
	
	public boolean login()
	{
		jlog.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		jlog.debug("login function");
		Encryptor en = new Encryptor();
		String hash = en.encryptPW(password);
		
		String[] result = ubdao.login(username, hash);
		
		if (result[4] == "true")
		{
			this.fullname = result[0];
			this.location = result[1];
			if(result[2] == "true")
			{
				this.admin = true;
			}
			else
			{
				this.admin = false;
			}
			if (result[3] == "true")
			{
				this.inactive = true;
			}
			else
			{
				this.inactive = false;
			}
			this.loggedIn = true;
			return true;
		}
		this.loggedIn = false;
		return false;
	}
	
	public boolean addUser(String username, String password, String fullname, String location, boolean admin, boolean inactive)
	{
		Encryptor encrypt = new Encryptor();
		String hash = encrypt.encryptPW(password);		

		if (ubdao.addUser(username, hash, fullname, location, admin, inactive))
		{
			return true;
		}
		jlog.info("addUser failed");
		return false;
	}
	
	public boolean deleteUser(String username)
	{
		//Database db = new Database("jdbc/waiDB");
		//UserBeanDAO ubdao = new UserBeanDAO();
		if(ubdao.deleteUser(username))
		{
			return true;
		}
		jlog.info("deleteUser failed");
		return false;
	}
	
	public boolean updateUser(String username, String typ, String value)
	{
		if (ubdao.updateUser(username, typ, value))
		{
			return true;
		}
		jlog.info("updateUser failed");
		return false;
	}

	public void logOut()
	{
		loggedIn = false;
	}
	
	public ArrayList<UserBean> getUsrLst()
	{
		usrLst = new ArrayList<UserBean>();
		ArrayList<String[]> newUsrLst = ubdao.getUsrLst();
//		String[] stringArray = new String[5];
		if(newUsrLst  == null)
		{
			return null;
		}
		else
		{
			
//			while( newUsrLst.iterator().hasNext());
			for(String[] stringArray: newUsrLst)
			{
				UserBean nxtUsr = new UserBean();
				nxtUsr.setUsername(stringArray[0]);
				nxtUsr.setFullname(stringArray[1]);
				nxtUsr.setLocation(stringArray[2]);
				
//				nxtUsr.setAdmin(Boolean.getBoolean((stringArray[3])));
				if(stringArray[3] == "true")
					nxtUsr.setAdmin(true);
				else
					nxtUsr.setAdmin(false);
				
//				nxtUsr.setInactive(Boolean.getBoolean(stringArray[4]));
				if(stringArray[4] == "true")
					nxtUsr.setInactive(true);
				else
					nxtUsr.setInactive(false);
			
				usrLst.add(nxtUsr);
			}
			
		}
		
		return usrLst;
	}
	
	public UserBean getUserBean(String username)
	{
		UserBean UsrBn = new UserBean();
		
		ArrayList<Object> usrArgs = ubdao.getUsr(username);
		
		UsrBn.setLocation(usrArgs.get(0).toString());
		UsrBn.setAdmin(usrArgs.get(1).equals(true));
		UsrBn.setInactive(usrArgs.get(2).equals(true));
		return UsrBn;
	}
}

